package org.example.service.imp;

import org.example.dto.VideoParseRecordDTO;
import org.example.dto.VideoParseStatsDTO;
import org.example.entity.VideoParseRecord;
import org.example.mapper.VideoParseRecordMapper;
import org.example.service.VideoParseRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class VideoParseRecordServiceImpl implements VideoParseRecordService {
    
    @Autowired
    private VideoParseRecordMapper videoParseRecordMapper;
    
    @Override
    public List<VideoParseRecordDTO> getAllRecords() {
        List<VideoParseRecord> records = videoParseRecordMapper.selectList(null);
        return convertToDTO(records);
    }
    
    @Override
    public List<VideoParseRecordDTO> getRecordsByNickname(String nickname) {
        List<VideoParseRecord> records = videoParseRecordMapper.getRecordsByNickname(nickname);
        return convertToDTO(records);
    }
    
    @Override
    public List<VideoParseRecordDTO> getRecordsByParseStatus(Integer parseStatus) {
        List<VideoParseRecord> records = videoParseRecordMapper.getRecordsByParseStatus(parseStatus);
        return convertToDTO(records);
    }
    
    @Override
    public List<VideoParseRecordDTO> getRecordsByPlatform(String platform) {
        List<VideoParseRecord> records = videoParseRecordMapper.getRecordsByPlatform(platform);
        return convertToDTO(records);
    }
    
    @Override
    public List<VideoParseRecordDTO> getRecordsByNicknameAndStatus(String nickname, Integer parseStatus) {
        List<VideoParseRecord> records = videoParseRecordMapper.getRecordsByNicknameAndStatus(nickname, parseStatus);
        return convertToDTO(records);
    }
    
    @Override
    public List<VideoParseRecordDTO> getRecordsByNicknameAndPlatform(String nickname, String platform) {
        List<VideoParseRecord> records = videoParseRecordMapper.getRecordsByNicknameAndPlatform(nickname, platform);
        return convertToDTO(records);
    }
    
    @Override
    public List<VideoParseRecordDTO> getSuccessRecords() {
        List<VideoParseRecord> records = videoParseRecordMapper.getSuccessRecords();
        return convertToDTO(records);
    }
    
    @Override
    public List<VideoParseRecordDTO> getFailedRecords() {
        List<VideoParseRecord> records = videoParseRecordMapper.getFailedRecords();
        return convertToDTO(records);
    }
    
    @Override
    public List<VideoParseRecordDTO> getRecordsWithPage(Integer page, Integer size) {
        int offset = (page - 1) * size;
        List<VideoParseRecord> records = videoParseRecordMapper.getRecordsWithPage(offset, size);
        return convertToDTO(records);
    }
    
    @Override
    public VideoParseStatsDTO getStats() {
        VideoParseStatsDTO stats = new VideoParseStatsDTO();
        
        Integer totalRecords = videoParseRecordMapper.getTotalCount();
        Integer successCount = videoParseRecordMapper.getSuccessCount();
        Integer failedCount = videoParseRecordMapper.getFailedCount();
        
        stats.setTotalRecords(totalRecords);
        stats.setSuccessCount(successCount);
        stats.setFailedCount(failedCount);
        
        if (totalRecords > 0) {
            stats.setSuccessRate((double) successCount / totalRecords * 100);
        } else {
            stats.setSuccessRate(0.0);
        }
        
        // 获取最近的记录
        List<VideoParseRecordDTO> recentRecords = getRecordsWithPage(1, 10);
        stats.setRecords(recentRecords);
        
        return stats;
    }
    
    @Override
    public VideoParseStatsDTO getStatsByNickname(String nickname) {
        VideoParseStatsDTO stats = new VideoParseStatsDTO();
        
        Integer totalRecords = videoParseRecordMapper.getCountByNickname(nickname);
        List<VideoParseRecord> successRecords = videoParseRecordMapper.getRecordsByNicknameAndStatus(nickname, 1);
        List<VideoParseRecord> failedRecords = videoParseRecordMapper.getRecordsByNicknameAndStatus(nickname, 0);
        
        Integer successCount = successRecords.size();
        Integer failedCount = failedRecords.size();
        
        stats.setTotalRecords(totalRecords);
        stats.setSuccessCount(successCount);
        stats.setFailedCount(failedCount);
        
        if (totalRecords > 0) {
            stats.setSuccessRate((double) successCount / totalRecords * 100);
        } else {
            stats.setSuccessRate(0.0);
        }
        
        // 获取该用户的记录
        List<VideoParseRecordDTO> userRecords = getRecordsByNickname(nickname);
        stats.setRecords(userRecords);
        
        return stats;
    }
    
    @Override
    public VideoParseStatsDTO getStatsByPlatform(String platform) {
        VideoParseStatsDTO stats = new VideoParseStatsDTO();
        
        Integer totalRecords = videoParseRecordMapper.getCountByPlatform(platform);
        List<VideoParseRecord> successRecords = videoParseRecordMapper.getRecordsByParseStatus(1);
        List<VideoParseRecord> failedRecords = videoParseRecordMapper.getRecordsByParseStatus(0);
        
        // 过滤指定平台的记录
        successRecords = successRecords.stream()
                .filter(record -> platform.equals(record.getPlatform()))
                .collect(Collectors.toList());
        failedRecords = failedRecords.stream()
                .filter(record -> platform.equals(record.getPlatform()))
                .collect(Collectors.toList());
        
        Integer successCount = successRecords.size();
        Integer failedCount = failedRecords.size();
        
        stats.setTotalRecords(totalRecords);
        stats.setSuccessCount(successCount);
        stats.setFailedCount(failedCount);
        
        if (totalRecords > 0) {
            stats.setSuccessRate((double) successCount / totalRecords * 100);
        } else {
            stats.setSuccessRate(0.0);
        }
        
        // 获取该平台的记录
        List<VideoParseRecordDTO> platformRecords = getRecordsByPlatform(platform);
        stats.setRecords(platformRecords);
        
        return stats;
    }
    
    /**
     * 将实体转换为DTO
     */
    private List<VideoParseRecordDTO> convertToDTO(List<VideoParseRecord> records) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        
        return records.stream().map(record -> {
            VideoParseRecordDTO dto = new VideoParseRecordDTO();
            dto.setId(record.getId());
            dto.setNickname(record.getNickname());
            dto.setOriginalUrl(record.getOriginalUrl());
            dto.setParseStatus(record.getParseStatus());
            dto.setParsedVideoUrl(record.getParsedVideoUrl());
            dto.setPlatform(record.getPlatform());
            
            if (record.getCreateTime() != null) {
                dto.setCreateTime(sdf.format(record.getCreateTime()));
            }
            if (record.getUpdateTime() != null) {
                dto.setUpdateTime(sdf.format(record.getUpdateTime()));
            }
            
            // 设置状态文本
            if (record.getParseStatus() != null) {
                switch (record.getParseStatus()) {
                    case 0:
                        dto.setStatusText("解析失败");
                        break;
                    case 1:
                        dto.setStatusText("解析成功");
                        break;
                    default:
                        dto.setStatusText("未知状态");
                }
            }
            
            // 设置平台图标
            if (record.getPlatform() != null) {
                switch (record.getPlatform()) {
                    case "抖音":
                        dto.setPlatformIcon("🎵");
                        break;
                    case "快手":
                        dto.setPlatformIcon("⚡");
                        break;
                    default:
                        dto.setPlatformIcon("📱");
                }
            }
            
            return dto;
        }).collect(Collectors.toList());
    }
} 